package com.portal.logic;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

public class FeedbackDAO {

	//private String ip;
	private Connection connection;
	
	public FeedbackDAO(Connection connection) {
		//this.ip = ip;
		this.connection = connection;
	}
	
	
	//Create
	public void insert() {
		Feedback feedback = null;
		try {
			Object deserializedObject = SerializationUtil.deserialize("c:\\feedback.out");
			feedback = (Feedback) deserializedObject;
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		PreparedStatement preparedStatement = null;

		try {
			Statement st = connection.createStatement(); 
            st.executeUpdate("INSERT INTO tblFeedbacks " + 
                "VALUES ("+feedback.getId()+", '"+feedback.getFeedBack()+ "',"+feedback.getProductId()+","+feedback.getCustomerId()+" )"); 

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}

			//ConnectionManager.closeConnection(connection);
		}
	}

	public Feedback selectById(int id) {
		Feedback feedback = new Feedback();
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		try {
			preparedStatement = connection.prepareStatement("SELECT * FROM tblFeedbacks WHERE id = ?");
			preparedStatement.setInt(1, id);
			resultSet = preparedStatement.executeQuery();

			while (resultSet.next()) {
				feedback.setId(resultSet.getInt("id"));
				feedback.setFeedBack(resultSet.getString("feedback"));
				feedback.setProductId(Integer.parseInt(resultSet.getString("product_id")));
				feedback.setCustomerId(Integer.parseInt(resultSet.getString("customer_id")));
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			//ConnectionManager.closeConnection(connection);
		}

		return feedback;
	}

	public Feedback selectByName(String name) {
		Feedback feedback = new Feedback();
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		try {
			preparedStatement = connection.prepareStatement("SELECT * FROM tblFeedbacks WHERE name = ?");
			preparedStatement.setString(1, name);
			resultSet = preparedStatement.executeQuery();

			while (resultSet.next()) {
				feedback.setId(resultSet.getInt("id"));
				feedback.setFeedBack(resultSet.getString("feedback"));
				feedback.setProductId(Integer.parseInt(resultSet.getString("product_id")));
				feedback.setCustomerId(Integer.parseInt(resultSet.getString("customer_id")));
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			//ConnectionManager.closeConnection(connection);
		}

		return feedback;
	}	
	
	//Read All
	public List<Feedback> selectAll() {
		Statement statement = null;
		ResultSet resultSet = null;
		List<Feedback> feedbacks = new LinkedList<Feedback>();
		try {
			statement = connection.createStatement();
			resultSet = statement.executeQuery("SELECT * FROM tblFeedbacks");

			Feedback feedback = null;
			while(resultSet.next()){
				feedback = new Feedback();
				feedback.setId(resultSet.getInt("id"));
				feedback.setFeedBack(resultSet.getString("feedback"));
				feedback.setProductId(Integer.parseInt(resultSet.getString("product_id")));
				feedback.setCustomerId(Integer.parseInt(resultSet.getString("customer_id")));
				feedbacks.add(feedback);
			}

			/*resultSet.close();
			statement.close();*/
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			//ConnectionManager.closeConnection(connection);
		}
		System.out.println(feedbacks);
		return feedbacks;
	}

	public List<Feedback> selectFeedbacksByProductId(int productId) {
		Statement statement = null;
		ResultSet resultSet = null;
		List<Feedback> feedbacks = new LinkedList<Feedback>();
		try {
			statement = connection.createStatement();
			String sql = "SELECT * FROM tblFeedbacks where product_id = "+productId+" ";
			resultSet = statement.executeQuery(sql);
			System.err.println(sql);
			
			Feedback feedback = null;
			while(resultSet.next()){
				feedback = new Feedback();
				feedback.setId(resultSet.getInt("id"));
				feedback.setFeedBack(resultSet.getString("feedback"));
				feedback.setProductId(Integer.parseInt(resultSet.getString("product_id")));
				feedback.setCustomerId(Integer.parseInt(resultSet.getString("customer_id")));
				feedbacks.add(feedback);
			}

			/*resultSet.close();
			statement.close();*/
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			//ConnectionManager.closeConnection(connection);
		}
		System.out.println(feedbacks);
		return feedbacks;
	}

	
	//Update
	public void update() {
		Feedback feedback = null;
		try {
			Object deserializedObject = SerializationUtil.deserialize("c:\\editFeedback.out");
			feedback = (Feedback) deserializedObject;
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		PreparedStatement preparedStatement = null;

		try {
			preparedStatement = connection.prepareStatement("UPDATE tblFeedbacks SET " +
					"feedback = ?, product_id = ?, customer_id = ? WHERE id = ?");

			preparedStatement.setString(1, feedback.getFeedBack());
			preparedStatement.setInt(2, feedback.getProductId());
			preparedStatement.setInt(3, feedback.getCustomerId());
			
			preparedStatement.setInt(4, feedback.getId());
			preparedStatement.executeUpdate();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			//ConnectionManager.closeConnection(connection);
		}
	}

	//Delete
	public void delete(int id) {
		PreparedStatement preparedStatement = null;

		try {
			preparedStatement = connection.prepareStatement("DELETE FROM tblFeedbacks WHERE id = ?");
			preparedStatement.setInt(1, id);
			preparedStatement.executeUpdate();

			System.out.println("DELETE FROM tblFeedbacks WHERE id = ?");

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			//ConnectionManager.closeConnection(connection);
		}
	}

	
	public int selectMaxId() {
		Statement statement = null;
		ResultSet resultSet = null;
		int id = 0;
		try {
			statement = connection.createStatement();
			resultSet = statement.executeQuery("SELECT max(id) as max_val FROM tblFeedbacks");

			while(resultSet.next()){
				id = Integer.parseInt(resultSet.getString("max_val"));
			}

			/*resultSet.close();
			statement.close();*/
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			//ConnectionManager.closeConnection(connection);
		}
		
		return id;
	}
}
